﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Fodee.Common;
using Fodee.DataEntity;
using Fodee.BusinessComponent;

namespace Fodee
{
    public partial class PeopleSel : Form
    {
        public event EventHandler<DataGridViewCellEventArgs> OnPersonSelected;
        public event EventHandler OnClean;
        public PeopleSel()
        {
            InitializeComponent();
        }
        public PeopleSel(Constants.PersonSearchType type)
        {
            InitializeComponent();
            this._type = type;
        }

        private Constants.PersonSearchType _type;

        public Constants.PersonSearchType PersonType
        {
            get { return _type; }
            set { _type = value; }
        }
	

        private void Bind(string searchCondition)
        {
            switch (this.PersonType)
            {
                case Constants.PersonSearchType.Staff:
                    FodeeDataSet dsStaff = new StaffBC().SearchStaff("%" + searchCondition + "%", 1);
                    grdPerson.AutoGenerateColumns = false;
                    grdPerson.Columns[1].DataPropertyName = "StaffName";
                    grdPerson.Columns[2].DataPropertyName = "StaffNo";
                    grdPerson.DataSource = dsStaff.fod_Staff;
                    break;
                case Constants.PersonSearchType.Member:
                    break;
                default:
                    break;
            }
        }

        private void btnClean_Click(object sender, EventArgs e)
        {
            if (this.OnClean != null)
            {
                this.OnClean(sender, e);
            }
            this.Close();
        }

        private void grdPerson_CellClick_1(object sender, DataGridViewCellEventArgs e)
        {
            if (this.OnPersonSelected != null)
            {
                this.OnPersonSelected(sender, e);
            }
            this.Close();
        }

        private void PeopleSel_Load(object sender, EventArgs e)
        {
            this.Bind(string.Empty);
            this.txtSearchCondition.Focus();
        }

        private void txtSearchCondition_TextChanged(object sender, EventArgs e)
        {
            this.Bind(txtSearchCondition.Text);
        }

        private void txtSearchCondition_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                OpenTable ot = (OpenTable)this.Owner;

                string id = this.grdPerson.SelectedRows[0].Cells[0].Value.ToString();
                string name = this.grdPerson.SelectedRows[0].Cells[1].Value.ToString();
                
                DataTable dt = new DataTable();
                dt.Columns.Add("Id", typeof(int));
                dt.Columns.Add("Name", typeof(string));
                dt.Rows.Add(new object[] { id, name });


                if (this.PersonType == Constants.PersonSearchType.Staff)
                {
                    ComboBox cbxWaiter = ((ComboBox)ot.Controls["cbxWaiter"]);
                    cbxWaiter.DataSource = null;
                    cbxWaiter.DisplayMember = "Name";
                    cbxWaiter.ValueMember = "Id";
                    cbxWaiter.DataSource = dt;
                    cbxWaiter.SelectedIndex = 0;
                }
                else if (this.PersonType == Constants.PersonSearchType.Member)
                {
                    ComboBox cbxCust = ((ComboBox)ot.Controls["cbxCust"]);
                    cbxCust.DataSource = null;
                    cbxCust.DisplayMember = "Name";
                    cbxCust.ValueMember = "Id";
                    cbxCust.DataSource = dt;
                    cbxCust.SelectedIndex = 0;
                }
                this.Close();
            }
        }
    }
}